package com.l.d.app.library.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.util.Set;

/**
 * Created by Administrator on 2016/9/23.
 *
 * @author LinDe
 */
public final class SharedPreferencesUtil
{
    private static final String DEFAULT_SHARED_PREFERENCES_FILE_NAME = ".d_preferences";

    public static <T> T getValue(Context context, @NonNull String key, @NonNull T defValue) {
        return getValue(DEFAULT_SHARED_PREFERENCES_FILE_NAME, context, key, defValue);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getValue(@NonNull String spFileName, Context context, @NonNull String key, @NonNull T defValue) {
        if (context == null)
            return defValue;
        SharedPreferences sp = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        T result = null;
        if (defValue instanceof String) {
            String s = sp.getString(key, (String) defValue);
            result = (T) s;
        } else if (defValue instanceof Integer) {
            Integer i = sp.getInt(key, (Integer) defValue);
            result = (T) i;
        } else if (defValue instanceof Float) {
            Float f = sp.getFloat(key, (Float) defValue);
            result = (T) f;
        } else if (defValue instanceof Long) {
            Long l = sp.getLong(key, (Long) defValue);
            result = (T) l;
        } else if (defValue instanceof Boolean) {
            Boolean b = sp.getBoolean(key, (Boolean) defValue);
            result = (T) b;
        } else if (defValue instanceof Set) {
            Set<String> set = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                set = sp.getStringSet(key, (Set<String>) defValue);
            }
            result = (T) set;
        }
        return result;
    }

    public static <T> boolean setValue(Context context, String key, T value) {
        return setValue(DEFAULT_SHARED_PREFERENCES_FILE_NAME, context, key, value);
    }

    public static <T> boolean setValue(String spFileName, Context context, String key, T value) {
        if (context == null || TextUtils.isEmpty(spFileName) || TextUtils.isEmpty(key))
            return false;

        SharedPreferences sp = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = sp.edit();

        boolean putSuccess = true;
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Set) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                //noinspection unchecked
                editor.putStringSet(key, (Set<String>) value);
            }
        } else {
            putSuccess = false;
        }
        if (putSuccess) {
            editor.apply();
        }
        return putSuccess;
    }
}
